package com.cyss.greenball.main;

import com.cyss.greenball.util.ActivityUtil;
import com.cyss.greenball.util.SystemUtil;
import com.lianzt.commondata.AbstractCommonData;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

public abstract class BaseActivity extends Activity {

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			AbstractCommonData acd = null;
			try {
				acd = (AbstractCommonData) msg.obj;
			} catch (Exception e) {
				Log.w(SystemUtil.LOG_TAG, "type must be AbstractCommonData");
				e.printStackTrace(); 
			}
			AbstractCommonData head = acd.getDataValue("head");
			try {
				int activityMode = head.getIntValue("activity_mode");
				switch (activityMode) {
				case ActivityUtil.ACTIVITY_MODE_FINISH:
					BaseActivity.this.finish();
					break;
				case ActivityUtil.ACTIVITY_MODE_RESUME:
					BaseActivity.this.onRestart();
					break;
				default:
					break;
				}
			} catch (Exception e) {
			}
			try {
				int activityFunction = head.getIntValue("activity_function");
				switch (activityFunction) {
				case ActivityUtil.TOAST:
					ActivityUtil.Toast(BaseActivity.this, acd);
					break;
				case ActivityUtil.DIALOG:
					ActivityUtil.Dialog(BaseActivity.this, acd);
					break;
				default:
					break;
				}
			} catch (Exception e) {
			}
		}
	};

	public void onCreate(Bundle savedInstanceState, int layout) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(layout);
		init();
		setOnListener();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}

	// @Override
	// public boolean onKeyDown(int keyCode, KeyEvent event) {
	// return true;
	// }

	protected abstract void init();

	protected abstract void setOnListener();
}
